/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta.components.unit;

import de.swta.components.category.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Jan Korreck
 */
public class UnitManager {

    private static UnitManager INSTANCE = new UnitManager();


    private HashMap<String, Unit> allUnits = new HashMap<String, Unit>();

    public static UnitManager getInstance() {
        return INSTANCE;
    }

    public void addUnit(Unit newUnit) {

        System.out.println("BEFORE: " + this.allUnits.keySet().size() + "(" + newUnit.getName() + ")");
        if (!this.allUnits.containsKey(newUnit.getName())) {
            this.allUnits.put(newUnit.getName(), newUnit);
        }
        System.out.println("AFTER: " + this.allUnits.keySet().size());
    }

    public void removeUnit(Unit newUnit) {
        this.allUnits.remove(newUnit.getName());
    }

    public void removeUnit(String id) {
        if (allUnits.containsKey(id)) {
            this.allUnits.remove(id);
        }
    }

    public Unit getUnit(String name) {
        return this.allUnits.get(name);
    }

    public boolean hasUnit(String catID) {
        return this.allUnits.containsKey(catID);
    }

    public List<Unit> getAllUnits() {
        return new ArrayList(this.allUnits.values());
    }

    public List<String> getUnitNames() {
        ArrayList<String> names = new ArrayList<String>();


        Iterator<Unit> valIt = this.allUnits.values().iterator();

        while(valIt.hasNext()) {
            names.add(valIt.next().getName());
        }

        return names;
    }

    public int getSize() {
        return this.allUnits.size();
    }

    public List<Unit> find(int first, int count, String sortProperty, boolean sortAsc) {

        List<Unit> subList = getIndex(sortProperty, sortAsc).subList(first, first + count);

        return subList;
    }

    private List<Unit> getIndex(String sortProperty, boolean sortAsc) {
        // hier müsste die DB gefragt werden und dementsprechend sortiert werden

        if (sortProperty == null) {
            return new ArrayList<Unit>(this.allUnits.values());

        } else if (sortProperty.equals("name")) {

            return this.sortByName(new ArrayList<Unit>(this.allUnits.values()), sortAsc);

        }

        throw new RuntimeException("unknown sort option [" + sortProperty +
                "]. valid options: [id] , [name]");
    }

    private List<Unit> sortByName(ArrayList<Unit> units, boolean asc) {

        if (asc) {
            Collections.sort(units, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Unit)o1).getName().compareTo(((Unit)o2).getName());
                }
            });
        } else {
            Collections.sort(units, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Unit)o2).getName().compareTo(((Unit)o1).getName());
                }
            });
        }
        return units;
    }
}
